package com.dao;

import com.entity.Grade;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class GradeDaoImpl implements GradeDao{

    @Override
    public List<Grade> getAll() {
        List<Grade> grades = new ArrayList<>();
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        Connection connection = null;
        PreparedStatement pst = null;
        ResultSet rs = null;
        try {
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/myschool?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT-8",
                    "root","root");
            pst = connection.prepareStatement("select * from grade");
            rs = pst.executeQuery();
            while (rs.next()){
                Grade grade = new Grade();
                grade.setGradeId(rs.getInt(1));
                grade.setGradeName(rs.getString(2));
                grades.add(grade);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            try {
                rs.close();
                pst.close();
                connection.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }


        return grades;
    }

    @Override
    public List<Grade> getByName(String name) {
        return null;
    }

    @Override
    public Grade getOne(Integer id) {
        return null;
    }

    @Override
    public int add(Grade grade) {
        return 0;
    }

    @Override
    public int update(Grade grade) {
        return 0;
    }

    @Override
    public int delete(Integer id) {
        return 0;
    }
}
